//
//  ViewController.swift
//  BDMap
//
//  Created by 尚甲荣 on 15/8/13.
//  Copyright (c) 2015年 尚甲荣. All rights reserved.
//

import UIKit

class ViewController: UIViewController,BMKMapViewDelegate,BMKLocationServiceDelegate{

    var locService : BMKLocationService!
    var isFirst :Bool = true
    /// 当前用户位置
    var userLocation: BMKUserLocation!
    
    @IBOutlet var myLocationBtn: UIButton!
    @IBOutlet var mapView: BMKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()
        myLocationBtn.layer.masksToBounds = true
        myLocationBtn.layer.cornerRadius = 3
        myLocationBtn.layer.borderWidth = 1
        myLocationBtn.layer.borderColor = UIColor.blueColor().CGColor
        mapView.zoomEnabled = true
        mapView.zoomLevel = 18
        startLocation()
    }
    
 
    @IBAction func myLocation(sender: AnyObject) {
        if userLocation != nil {
         mapView.centerCoordinate = userLocation.location.coordinate
        }
        
    }
    
    
    
    
    func startLocation(){
    
        BMKLocationService.setLocationDesiredAccuracy(kCLLocationAccuracyNearestTenMeters)
        BMKLocationService.setLocationDistanceFilter(10)
        locService = BMKLocationService()
        locService.startUserLocationService()
        mapView.showsUserLocation = false
        mapView.userTrackingMode = BMKUserTrackingModeNone
        mapView.showsUserLocation = true
      
    }

    
    func didUpdateUserHeading(userLocation: BMKUserLocation!) {
        mapView.updateLocationData(userLocation)
//        mapView.centerCoordinate  = userLocation.location.coordinate
    }
    func didUpdateBMKUserLocation(userLocation: BMKUserLocation!) {
        mapView.updateLocationData(userLocation)
        self.userLocation = userLocation
        mapView.centerCoordinate  = userLocation.location.coordinate
        if isFirst{
          isFirst = false
          addAnnotations()
        }
        
    }

    //108.902033,34.241048    108.901629,34.241298
    
    
    //增加标注
    func addAnnotations(){
    
        var annotation = MyAnnotation()
        var coordinator = CLLocationCoordinate2DMake(34.241048,108.902033)
        annotation.coordinate = coordinator
        annotation.title = "高新四路一号"
        annotation.subtitle = "高新西路180号........"
        annotation.index = 1
        mapView.addAnnotation(annotation)

    }
    
    class MyAnnotation: BMKPointAnnotation {
      
        var index : Int!
    }
  
    
    //绘制标注
    func mapView(mapView: BMKMapView!, viewForAnnotation annotation: BMKAnnotation!) -> BMKAnnotationView! {
        var AnnotationViewID = "renameMark"
        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(AnnotationViewID) as! BMKPinAnnotationView?
        if annotationView == nil {
            annotationView = BMKPinAnnotationView(annotation: annotation, reuseIdentifier: AnnotationViewID)
            // 设置颜色
            annotationView!.pinColor = UInt(BMKPinAnnotationColorPurple)
            // 从天上掉下的动画
            annotationView!.animatesDrop = true
            // 设置可拖曳
            annotationView!.draggable = true
        }
        return annotationView

    }
    
    func mapView(mapView: BMKMapView!, annotationView view: BMKAnnotationView!, didChangeDragState newState: UInt, fromOldState oldState: UInt) {
      
    }
    
    //当点击annotation view弹出的泡泡时，调用此接口
    func mapView(mapView: BMKMapView!, annotationViewForBubble view: BMKAnnotationView!) {
        let s = view.annotation.coordinate.latitude
        if (view.annotation.coordinate.longitude == self.userLocation.location.coordinate.longitude)&&(view.annotation.coordinate.latitude == self.userLocation.location.coordinate.latitude) {
            
        }else{
            
            let a = view.annotation as! MyAnnotation
            println("##############\(a.index)")
            
        }
    }
    
    //当选中一个annotation views时，调用此接口
    func mapView(mapView: BMKMapView!, didSelectAnnotationView view: BMKAnnotationView!) {
      
       
        
       
    }
    
   
    
    override func viewWillAppear(animated: Bool) {
        mapView.delegate = self
        locService.delegate = self
    }
    
    override func viewWillDisappear(animated: Bool) {
        mapView.delegate = nil
        locService.delegate = nil
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}

